
TheGame = 
{
    --The main game object
    myGame,
    --The audio engine
    myAudio,
    --The currently selected BPHysicsActor
    myActor,
    --AI variables
    SeekKey = 0,
    FleeKey = 1,
    PathKey = 2,

    Print =     function(string)
                    TheEngine.Print(string)
                end,

    LoadFullTextures = function()
                        TheGame.Print("Loading Textures...0%...\n")
                        local path = "Textures/"
                        TheEngine.engine:LLoadTexture(path, "car.jpg")
                        TheEngine.engine:LLoadTexture(path, "policeCar.jpg")
                        TheEngine.engine:LLoadTexture(path, "cube.jpg")
                        TheEngine.engine:LLoadTexture(path, "tyreTexture.png")                        
                        --City
                        path = "Meshes/Block_0/"
                        TheEngine.engine:LLoadTexture(path, "concre0.jpg")
                        TheEngine.engine:LLoadTexture(path, "curb_d0.jpg")
                        TheEngine.engine:LLoadTexture(path, "dirtma3.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_021.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_030.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_060.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_070.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_081.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_101.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_110.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_121.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_170.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_180.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_191.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_261.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_270.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_291.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro1.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro2.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro3.jpg")
                        TheGame.Print("50%...")
                        TheEngine.engine:LLoadTexture(path, "flatro4.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro5.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro6.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro7.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro8.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro9.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin0.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin2.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin3.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin4.jpg")
                        TheEngine.engine:LLoadTexture(path, "paveme0.jpg")
                        TheEngine.engine:LLoadTexture(path, "road_d0.jpg")
                        TheEngine.engine:LLoadTexture(path, "roofti3.jpg")
                        TheEngine.engine:LLoadTexture(path, "roofti6.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_10.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_20.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_60.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_80.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_90.jpg")
                        --the 6 textures for the cubemap
                        path = "Textures/SkyBox2/"
                        TheEngine.engine:LLoadTexture(path, "box_floor.png")
                        TheEngine.engine:LLoadTexture(path, "box_top.png")
                        TheEngine.engine:LLoadTexture(path, "box_0.png")
                        TheEngine.engine:LLoadTexture(path, "box_1.png")
                        TheEngine.engine:LLoadTexture(path, "box_2.png")
                        TheEngine.engine:LLoadTexture(path, "box_3.png")
                        --finished
                        TheGame.Print("Loading Textures...complete")
                        end,

    LoadCity =  function()
                        TheGame.Print("Loading City...0%")
                        local path = "Block_0/"
                        TheGame.myGame:LLoadBlock("Block_0/", "floor", false)
                        TheGame.myGame:LLoadBlock("Block_0/", "curb", false)
                        TheGame.myGame:LLoadBlock(path, "building_00", true)
                        TheGame.myGame:LLoadBlock(path, "building_01", true)
                        TheGame.myGame:LLoadBlock(path, "building_02", true)
                        TheGame.myGame:LLoadBlock(path, "building_03", true)
                        TheGame.Print("...25%...")
                        TheGame.myGame:LLoadBlock(path, "building_04", true)
                        TheGame.myGame:LLoadBlock(path, "building_05", true)
                        TheGame.myGame:LLoadBlock(path, "building_06", true)
                        TheGame.myGame:LLoadBlock(path, "building_07", true)
                        TheGame.myGame:LLoadBlock(path, "building_08", true)
                        TheGame.Print("...50%...")
                        TheGame.myGame:LLoadBlock(path, "building_09", true)
                        TheGame.myGame:LLoadBlock(path, "building_10", true)
                        TheGame.myGame:LLoadBlock(path, "building_11", true)
                        TheGame.myGame:LLoadBlock(path, "building_12", true)
                        TheGame.myGame:LLoadBlock(path, "building_13", true)
                        TheGame.Print("...75%...")
                        TheGame.myGame:LLoadBlock(path, "building_14", true)
                        TheGame.myGame:LLoadBlock(path, "building_15", true)
                        TheGame.myGame:LLoadBlock(path, "building_16", true)
                        TheGame.myGame:LLoadBlock(path, "building_17", true)
                        TheGame.Print("Loading City...100%")
                    end,

    LoadTestLevel = function()
                        --make a plane
                        TheGame.Print("Creating Plane...")
                        local length = 512
                        TheGame.myGame:LCreatePlaneActor(0,1,0, 0.01, length, -0.01,length)
                        --make a couple of spheres
                        local numSpheres = 10
                        local angle = 0
                        local inc = 360 / numSpheres
                        local radius = 50
                        TheGame.Print("Creating Spheres...")
                        for index = 1, numSpheres, 1 do
                            TheGame.myGame:LCreateSphereActor(7,math.cos(angle*math.pi/180)*radius,-1,
                                                                math.sin(angle*math.pi/180)*radius)
                            angle = angle + inc
                            radius = radius + 15
                        end
                        --make some walls!
                        TheGame.Print("Creating Walls...")
                        TheGame.myGame:LCreateWallActor(length, 5, length,
                                                        length, 5, -length,2)
                        TheGame.myGame:LCreateWallActor(-length, 5, -length,
                                                        -length, 5,  length,2)
                        TheGame.myGame:LCreateWallActor(length, 5, length,
                                                       -length, 5,  length,2)
                        TheGame.myGame:LCreateWallActor(-length, 5, -length,
                                                        length, 5,  -length,2)
                        TheGame.Print("Test level created...")
                    end,

    LoadTextures = function()
                        --Default is allready loaded in
                        --TheGame.LoadFullTextures()
                        local path = "Textures/"
                        TheEngine.engine:LLoadTexture(path, "audioDebug.png")
                    end,

    LoadSharedMeshes = function()
                            TheGame.Print("Loading Meshes...")
                            local path = "Meshes/"
                            TheEngine.engine:LLoadSharedMesh(path, "bunny")
                            TheEngine.engine:LLoadSharedMesh(path, "teapot")
                            TheEngine.engine:LLoadSharedMesh(path, "tyreMesh")
                            TheEngine.engine:LLoadSharedMesh(path, "car")
                            TheEngine.engine:LLoadSharedMesh(path, "car_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "teapot_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "bunny_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "unitSphere")
                            TheEngine.engine:LLoadSharedMesh(path, "unit_hemisphere")
                            TheGame.Print("Loading Meshes...Complete")
                        end,

    Reset =         function()
                        TheGame.Print("Game Reseting...")
                        TheGame.LoadCity()
                        --TheGame.LoadTestLevel()
                        TheGame.CreateSamples()
                        TheGame.Print("Reset Complete")
                    end,

    SetActor =      function(actor)
                        TheGame.myActor = actor
                        TheGame.Print("Actor Set")
                    end,

    InitialiseGame =    function()
                        TheGame.Print("Initialising Game Components...")
                        TheGame.Print("Creating Buffers...")
                        TheGame.Print("Connecting Broadphase...")
                        TheGame.Print("Compiling Shaders...")
                        TheGame.Print("Parsing Scripts...")
                        --Overriden if using Full screen
                        TheEngine.OpenFile("Scripts/","carOne.lua")
                        TheEngine.OpenFile("Scripts/","carTwo.lua")
                        TheEngine.OpenFile("Scripts/","carThree.lua")
                        TheEngine.OpenFile("Scripts/","car_states.lua")
                        TheGame.LoadTextures()
                        --populate AI map
                        TheGame.Print("Populating Color key map..")
                        TheGame.myGame:LPopulateColorKey(0, 0,0,0)
                        TheGame.myGame:LPopulateColorKey(1, 1,0,0)
                        --populate0
                        TheGame.Print("Populating Behaviour Map")
                        TheGame.myGame:LSetSeekKey(TheGame.SeekKey)
                        TheGame.myGame:LSetFleeKey(TheGame.FleeKey)
                        TheGame.myGame:LSetPathKey(TheGame.PathKey)
                        TheGame.Print("Creating Sky dome...")
                        TheGame.myGame:LInitialiseSkyDome(0,128,0, 1024)
                        TheGame.Print("Loading Resources...")
                        TheGame.LoadSharedMeshes()
                        TheGame.Print("Game Started!")
                    end,

    InitialiseAudio = function(soundEngine)
                        TheGame.Print("Initialising Sound engine...")
                        TheGame.myAudio = soundEngine
                        --frames per buffer, channels, sample rate -- deley(secs), maxsounds
                        TheGame.myAudio:LInitialiseEngine(128,2,42100,2, 16)
                        local source = 
                        {
                            path = "Sounds/",
                            name = "Play.wav",
                            volume = 1.0,
                        }
                        TheGame.myAudio:LCreateSource(source)
                        source.name = "noo.wav"
                        TheGame.myAudio:LCreateSource(source)
                        source.name = "shot.wav"
                        TheGame.myAudio:LCreateSource(source)
                        source.name = "bikeEngine.wav"
                        TheGame.myAudio:LCreateSource(source)

                        TheGame.Print("Sound engine Ready!...")
                    end,

    CreateSamples =     function()
                            TheGame.Print("Calling CreateSamples")
                            TheGame.myAudio:LCreate3DSound("shot.wav",-40,75,-50, 1000)
                        end,

    InitialiseAI   =function(ai)
                        TheGame.Print("Initialising AI Perception System")
                        --Texture X,Y -- Projection H, W, -- Projection N,F
                        --ai:LSetUpPerceptionOrthographic(32,8,6,8,1,100)
                        local pi = math.pi
                        local fov = pi/16
                        --Texture X,Y -- Proj: Feild of View, Aspect -- Proj:N,F
                        ai:LSetUpPerceptionPerspective(32,8, fov, 4, 8, 256)
                    end,

    Start =         function(game)
                        TheGame.myGame = game
                        TheGame.InitialiseGame()
                    end
}